

jQuery(function ($) {
    var user_info = JSON.parse(localStorage.getItem('user_info'));
    if (!user_info) {
        window.location.href = "/login/login.html";
    }
    var id = user_info.id;
    var addrs;
    var addrClick;
    addrsList();
    function addrsList() {
        $('.user_addr_list').html('');
        var addressList = {
            "UserAddress[]": {
                "UserAddress": {
                    "user_id": id
                }
            }
        };
        if (id) {
            send_ajax('post', '/get', addressList, function callback(data) {
                if (data.code == 200 && data["UserAddress[]"]) {
                    addrs = data["UserAddress[]"];
                    for (var i = 0; i < addrs.length; i++) {
                        joint_address(addrs[i]);
                    }
                } else {
                    return;
                };
            });
        }
    }
    //删除地址
    $(document).on('click', '.delete_addres', function () {
        var adds_id = $(this).attr('data-addres-id');
        var param = {
            "UserAddress": {
                "id": adds_id
            }
        };
        send_ajax('post', '/delete', param, function callback(data) {
            if (data.code == 200) {
                alert('删除成功');
                addrsList();
            }
        });
    });
    //设置默认地址
    $(document).on('click', '.default_addres', function () {
        var addr_id = $(this).attr('data-addres-id');
        var param = {
            "address_id": addr_id,
            "user_id": id
        };
        send_ajax('post', '/api/user/address_default', param, function callback(result) {
            if (result.code == 200) {
                alert('设置成功');
            }
        });
    });
    //拼接购物地址
    function joint_address(address) {
        var area_ids = {
            "Area[]": {
                "count": 10,
                "Area": {
                    "use_flag": 1,
                    "id{}": [address.province, address.city, address.district, address.street]
                }
            }
        };
        send_ajax('post', '/get', area_ids, function callback(data) {
            if (data.code == 200&&data["Area[]"]) {
                //修改主数据
                var addr_info = data["Area[]"];
                address.province_obj = addr_info[0];
                address.city_obj = addr_info[1];
                address.district_obj = addr_info[2];
                address.street_obj = addr_info[3];
            }
            $('.user_addr_list').append('<li data-addres-id="' + address.id + '">\n                                        <strong>' + address.receiver + '</strong>\n                                        <p><span>\u6536\u8D27\u4EBA:</span><span>' + address.receiver + '</span></p>\n                                        <p><span>\u6240\u5728\u5730\u533A:</span><span>' + ((address.province_obj ? address.province_obj.name : '') + (address.city_obj ? address.city_obj.name : '') + (address.district_obj ? address.district_obj.name : '') + (address.street_obj != undefined ? address.street_obj.name : "")) + '</span></p>\n                                        <p><span>\u5730\u5740:</span><span>' + ((address.province_obj ? address.province_obj.name : '') + (address.city_obj ? address.city_obj.name : '') + (address.district_obj ? address.district_obj.name : '') + (address.street_obj != undefined ? address.street_obj.name : "") + address.location) + '</span></p>\n                                        <p><span>\u624B\u673A:</span><span>' + address.phone + '</span></p>\n                                        <div>\n                                            <span class="default_addres" data-addres-id="' + address.id + '">\u8BBE\u7F6E\u4E3A\u9ED8\u8BA4\u5730\u5740</span>\n                                            <em>|</em>\n                                            <span class="modify_addres" data-addres-id="' + address.id + '">\u4FEE\u6539</span>\n                                            <em>|</em>\n                                            <span class="delete_addres" data-addres-id="' + address.id + '">\u5220\u9664</span>\n                                        </div>\n                                    </li>');
        });
    };
    //添加地址
    $('.add_address').click(function () {
        $("#dialog").dialog({
            // width:690,
            modal: true
        });
    });

    //保存收货地址
    var province_id = '';
    var city_id = '';
    var area_id = '';
    var town_id = '';
    var ismodifying = false;
    $('.save_information').click(function () {
        if($('.name_div button').hasClass('modifying')){
            ismodifying=true;
        }else{
            ismodifying = false
        }
        var reg = /[0-9]{6,20}/; //手机号码
        var province = $('.province').val();
        var city = $('.city').val();
        var area = $('.area').val();
        var town = $('.town').val();
        var receiver = $('.receiver').val();
        var detailed_txt = $('.detailed_txt ').val();
        var phone = $.trim($(".phone").val());
        //非空验证

        if (receiver == "") {
            $('.receiver_msg').show();
            $('.receiver_msg').html('请您填写收货人姓名');
            return;
        } else {
            $('.receiver_msg').hide();
        }
        if (detailed_txt == "") {
            $('.detailed_msg').show();
            $('.detailed_msg').html('请您填写收货人详细地址');
            return;
        } else {
            $('.detailed_msg').hide();
        }
        //手机号码
        if (phone == "") {
            $('.phone_msg').show();
            $('.phone_msg').html('请您填写收货人手机号码');
            return;
        } else if (reg.test(phone)) {
            $('.phone_msg').hide();
        } else {
            $('.phone_msg').show();
            $('.phone_msg').html('手机号格式不对，请重新输入');
            return;
        }
        //保存收货地址
        var flag;
        if ($('.checkbox').is(':checked')) {
            flag = 1;
        } else {
            flag = 0;
        };
        if (province != '' && city != '' && area != '' && detailed_txt != '' && receiver != '' && phone != '') {
            $('.localInfo').hide();
            if (!isLinkage) {
                var params = {
                    "UserAddress": {
                        "city": city_id,
                        "default_flag": flag,
                        "district": area_id,
                        "location": detailed_txt,
                        "phone": phone,
                        "province": province_id,
                        "receiver": receiver,
                        "street": town_id,
                        "user_id": id
                    },
                    "TAG": "UserAddress"
                };
            }else{
                var params = {
                    "UserAddress": {
                        "city": cityId,
                        "default_flag": flag,
                        "district": areaId,
                        "location": detailed_txt,
                        "phone": phone,
                        "province": provinceId,
                        "receiver": receiver,
                        "street": townId,
                        "user_id": id
                    },
                    "TAG": "UserAddress"
                };
            };
            if(ismodifying){
                params.UserAddress.id = addrs_id;
            }
            send_ajax('post', ismodifying ? '/put':'/post', params, function callback(result) {
                if (result.code == 200) {
                    if(ismodifying){
                        $('.name_div button').removeClass('modifying')
                    }
                    addrsList();
                    $("#dialog").dialog('close');
                    $('.receiver').val('');
                    $('.detailed_txt ').val('');
                    $(".phone").val('');
                }
            });
        }else {
            $('.localInfo').show();
            $('.localInfo').html('请您填写完整的地区信息');
        }
    });
    //关闭地址弹窗
    $('.cancel_address').click(function () {
        $("#dialog").dialog('close');
    });
    //修改地址
    var addrs_id;
    $(document).on('click', '.modify_addres', function () {
        $('.save_information').addClass('modifying');
        var html = "";
        addrs_id = $(this).attr('data-addres-id');
        addrClick = addrs.filter(function (el) {
            return el.id == addrs_id;
        });
        province_id = addrClick[0].province;
        city_id = addrClick[0].city;
        area_id = addrClick[0].district;
        town_id = addrClick[0].street;
        var city = addrClick[0].city_obj.name; //市
        var district = addrClick[0].district_obj.name;
        var province = addrClick[0].province_obj.name; //省
        if (addrClick[0].street_obj != undefined) {
            var street = addrClick[0].street_obj.name; //街道
            $('.town').show().removeAttr('disabled');;
        } else {
            var street = ""; //街道
            $('.town').hide();
        }
        var location = addrClick[0].location; //具体位置
        var receiver = addrClick[0].receiver; //接受者
        var phone = addrClick[0].phone; //电话
        $("#dialog").dialog({
            modal: true
        });
        var option_province = document.querySelector('.province option');
        var option_city = document.querySelector('.city option');
        var option_area = document.querySelector('.area option');
        var option_town = document.querySelector('.town option');
        $('.city').show().removeAttr('disabled');;
        $('.area').show().removeAttr('disabled');;

        $(option_province).text(province);
        $(option_city).text(city);
        $(option_area).text(district);
        $(option_town).text(street);

        $(option_province).val(addrClick[0].province);
        $(option_city).val(addrClick[0].city);
        $(option_area).val(addrClick[0].district);
        $(option_town).val(addrClick[0].street);
        $('.receiver').val(receiver);
        $('.detailed_txt').val(location);
        $('.phone').val(phone);
    });
});